package cn.wolfcode.p2p.interceptor;

import cn.wolfcode.p2p.base.anno.NeedLogin;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckLoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod method = (HandlerMethod) handler;
            boolean b = method.hasMethodAnnotation(NeedLogin.class);
            if(b && UserContext.getCurrentUser() == null){
                response.sendRedirect("/login.html");
                return false;
            }
        }
        return true;
    }
}
